#!/bin/bash

set -eu

orig_dir=$(cd $(dirname $0) && pwd -P)
tmpdir=$(mktemp -d)

cat <<EOF >$tmpdir/.coveragerc
[run]
parallel=true
data_file=$tmpdir/.coverage
EOF

cat <<EOF >$tmpdir/sitecustomize.py
import coverage
coverage.process_startup()
EOF

export COVERAGE_PROCESS_START=$tmpdir/.coveragerc
export PYTHONPATH=$tmpdir:

# Produce a coverage report, even if the tests fail
set +e
$orig_dir/run_tests
exitcode=$?
set -e

cd $tmpdir
coverage3 combine -q
coverage3 html -d $orig_dir/report
coverage3 report -m
cd $orig_dir
rm -rf $tmpdir

exit $exitcode
